#include <QtGui>
#include "memoMainWindow.h"
#include "ui_memoMainWindow.h"
#include "aboutDialog.h"
#include "memoDBConnection.h"
#include "memoStateEditWidget.h"
#include "memoQueryWidget.h"
#include "memoDetailInfoWidget.h"
#include "memoDetailInfoDialog.h"
#include "alarmDialogV1.h"

MemoMainWindow::MemoMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MemoMainWindow)
{
    ui->setupUi(this);

    initDBRelated();

    initUIs();

    initiateSystemTrayIcon();



}

MemoMainWindow::~MemoMainWindow()
{
    delete ui;
}

void MemoMainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MemoMainWindow::initUIs()
{
    m_pAlarmDialogV1 = new AlarmDialogV1(this->m_pMemoDBConnection->getConnection(),
                                         this);

    //connect uis' signals with slots
    connect(ui->ui_ActionExit,SIGNAL(triggered()),
            qApp,SLOT(quit()));

    connect(ui->ui_ActionAbout,SIGNAL(triggered()),
            this, SLOT(doActionAbout()));

    connect(ui->ui_ActionMemorandumLabelEditor,SIGNAL(triggered()),
            this,SLOT(doActionMemorandumLabelEditor()));

    connect(ui->ui_ActionAlarm,SIGNAL(toggled(bool)),
            this,SLOT(doActionAlarm(bool)));

    connect(this->m_pAlarmDialogV1, SIGNAL(WeCannotSeeIt(bool)),
            ui->ui_ActionAlarm, SLOT(setChecked(bool)));

    ui->ui_ActionAlarm->setChecked(false);//hide the alarm dialog

    //add toolbar show action to view menu
    ui->ui_MenuView->addAction(ui->ui_ToolBarFile->toggleViewAction());

    this->setWindowTitle(tr("Memorandum"));
    this->resize(800,600);


    //Central Widget
//    m_pCentralWidget = new QWidget(this);
    m_pCentralSpliter = new QSplitter(Qt::Horizontal, this);

    //MemoQueryWidget
    m_pMemoQueryWidget = new MemoQueryWidget(this->m_pMemoDBConnection->getConnection(), m_pCentralSpliter);
    m_pCentralSpliter->addWidget(m_pMemoQueryWidget);

    //right widget
    m_pRightWidget = new QWidget(m_pCentralSpliter);
    //MemoDetailInfoWidget
    m_pMemoDetailInfoWidget = new MemoDetailInfoWidget(this->m_pMemoDBConnection->getConnection(),
                                                       "",
                                                       MemoDetailInfoWidget::UNEDITABLE,
                                                       m_pRightWidget);
//
    m_strCurID = "";

    QVBoxLayout* _RightWidgetLayout = new QVBoxLayout;
//

    m_pBtnShow = new QPushButton("Show", m_pRightWidget);
    m_pBtnAddNew = new QPushButton("Add new", m_pRightWidget);
    m_pBtnDelete = new QPushButton("Delete", m_pRightWidget);
    m_pBtnModify = new QPushButton("Modify", m_pRightWidget);

    connect(m_pBtnShow, SIGNAL(clicked()),
            this, SLOT(doBtnShow()));
    connect(m_pBtnAddNew, SIGNAL(clicked()),
            this, SLOT(doBtnAddNew()));
    connect(m_pBtnModify, SIGNAL(clicked()),
            this, SLOT(doBtnModify()));
    connect(m_pBtnDelete, SIGNAL(clicked()),
            this, SLOT(doBtnDelete()));

    QHBoxLayout* _OPLayout = new QHBoxLayout;
    _OPLayout->addStretch(2);
    _OPLayout->addWidget(m_pBtnShow);
    _OPLayout->addWidget(m_pBtnAddNew);
    _OPLayout->addWidget(m_pBtnModify);
    _OPLayout->addWidget(m_pBtnDelete);
    _OPLayout->addStretch(2);
//
    _RightWidgetLayout->addWidget(m_pMemoDetailInfoWidget);
    _RightWidgetLayout->addLayout(_OPLayout);
//
    m_pRightWidget->setLayout(_RightWidgetLayout);

    m_pCentralSpliter->addWidget(m_pRightWidget);


    //
    this->setCentralWidget(m_pCentralSpliter);
    connect(m_pMemoQueryWidget, SIGNAL(activated(QString)),
            m_pMemoDetailInfoWidget, SLOT(fillDate(QString)));
    connect(m_pMemoQueryWidget, SIGNAL(activated(QString)),
            this, SLOT(IDChanged(QString)));

//    connect(m_pMemoQueryWidget, SIGNAL(doSearch()),
//            m_pMemoDetailInfoWidget, SLOT(clear()));


}


//slots
void MemoMainWindow::doActionAbout()
{
    AboutDialog _AboutDialog;
    _AboutDialog.exec();
}


void MemoMainWindow::doActionAlarm(bool _checked)
{
    if(_checked){
        this->m_pAlarmDialogV1->show();
        this->m_pAlarmDialogV1->raise();
        this->m_pAlarmDialogV1->activateWindow();
    }else{
        this->m_pAlarmDialogV1->hide();
    }
}




//------------------------------------------------------------------------------------------------------------------------
//QSystemTrayIcon
//------------------------------------------------------------------------------------------------------------------------
void MemoMainWindow::initiateSystemTrayIcon()
{
    m_pMinimizeAction = new QAction(tr("Mi&nimize"), this);
    connect(m_pMinimizeAction, SIGNAL(triggered()),
            this, SLOT(hide()));

    m_pMaximizeAction = new QAction(tr("Ma&ximize"), this);
    connect(m_pMaximizeAction, SIGNAL(triggered()),
            this, SLOT(showMaximized()));

    m_pRestoreAction = new QAction(tr("&Restore"), this);
    connect(m_pRestoreAction, SIGNAL(triggered()),
            this, SLOT(showNormal()));

    m_pQuitAction = new QAction(tr("&Quit"), this);
    connect(m_pQuitAction, SIGNAL(triggered()),
            qApp, SLOT(quit()));

    m_pTrayIconMenu = new QMenu(this);
    m_pTrayIconMenu->addAction(m_pMinimizeAction);
    m_pTrayIconMenu->addAction(m_pMaximizeAction);
    m_pTrayIconMenu->addAction(m_pRestoreAction);
    m_pTrayIconMenu->addSeparator();
    m_pTrayIconMenu->addAction(m_pQuitAction);

    m_pTrayIcon = new QSystemTrayIcon(this);
    m_pTrayIcon->setContextMenu(m_pTrayIconMenu);
    m_AppIcon = QIcon(":/AppIcon/AppIcon");
    m_AlarmIcon = QIcon(":/MainWindowIcons/Alarm");
    m_pTrayIcon->setIcon(m_AppIcon);
    m_pTrayIcon->setToolTip(tr("The App"));

    connect(m_pTrayIcon, SIGNAL(messageClicked()),
            this,SLOT(messageClicked()));

    connect(m_pTrayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));

    connect(this->m_pAlarmDialogV1,SIGNAL(ThereAreNewAlarms(int)),
            this, SLOT(ThereAreNewAlarms(int)));


    m_pTrayIcon->show();
}

void MemoMainWindow::closeEvent(QCloseEvent *event)
{
    if (m_pTrayIcon->isVisible()) {
//        QMessageBox::information(this, tr("Systray"),
//                                 tr("The program will keep running in the "
//                                    "system tray. To terminate the program, "
//                                    "choose <b>Quit</b> in the context menu "
//                                    "of the system tray entry."));
        hide();
        event->ignore();
    }
}

void MemoMainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{

    switch(reason){
    case QSystemTrayIcon::Trigger:
        //qDebug()<<"Trigger";
        if(this->isHidden()){
            //qDebug()<<"isVisible";
            this->show();
            if(m_AppState == ThereAreAlarms){
                this->m_pAlarmDialogV1->setShown(true);
            }else if(m_AppState == NoAlarm){
                this->m_pAlarmDialogV1->setShown(false);
            }else{
                this->m_pAlarmDialogV1->setShown(false);
            }
        }else{
            //qDebug()<<"not is Visible";
            this->hide();
            this->m_pAlarmDialogV1->setShown(false);
        }

        break;
    case QSystemTrayIcon::DoubleClick:
        //qDebug()<<"DoubleClick";
        break;
    default:
        ;

    }
}

void MemoMainWindow::ThereAreNewAlarms(int _dAlarmCount)
{
    if(_dAlarmCount>0){//There Are New Alarms
//        this->ui->ui_ActionAlarm->setChecked(true);
        m_AppState = ThereAreAlarms;
        m_pTrayIcon->setIcon(m_AlarmIcon);
        m_pTrayIcon->setToolTip(tr("There are %1 Alarm(s)").arg(_dAlarmCount));
    }else{//No Alarm
//        this->ui->ui_ActionAlarm->setChecked(false);
        m_AppState = NoAlarm;
        m_pTrayIcon->setIcon(m_AppIcon);
        m_pTrayIcon->setToolTip(tr("The App"));
    }
}

void MemoMainWindow::showMessage(int _dAlarmCount)
{
//    QString _str;
//    _str.append("There are <b>");
//    _str.append(QString(_dAlarmCount));
//    _str.append("</b> Alarm(s)\n Press to see detail");
    m_pTrayIcon->showMessage(tr(""),
                             tr("There are %1 Alarm(s)\n Press here to see detail").arg(_dAlarmCount),
                             QSystemTrayIcon::NoIcon);
}

void MemoMainWindow::messageClicked()
{
    this->show();
}

//------------------------------------------------------------------------------------------------------------------------
//DB Related
//------------------------------------------------------------------------------------------------------------------------
void MemoMainWindow::initDBRelated()
{
    m_pMemoDBConnection = new MemoDBConnection(this);
    if(!m_pMemoDBConnection->connect()){
        QMessageBox msgBox;
        msgBox.setIcon(QMessageBox::Critical);
        msgBox.setText("<b>can not Connect Memo Datebase<\b>");
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.exec();
        qApp->quit();
    }
}

void MemoMainWindow::doActionMemorandumLabelEditor()
{
    MemoStateEditWidget _dlg(this->m_pMemoDBConnection->getConnection(),this);
    _dlg.exec();
    m_pMemoQueryWidget->updateComboxMemoLabel();
    this->m_pMemoDetailInfoWidget->updateComboxMemoLabel();
    this->m_pAlarmDialogV1->updateComboxMemoLabel();
//    _dlg->show();
//    delete _dlg;
}

void MemoMainWindow::doBtnAddNew()
{
    MemoDetailInfoDialog _dlg(this->m_pMemoDBConnection->getConnection(),
                              "",
                              MemoDetailInfoDialog::New,
                              this);
    _dlg.exec();
    this->m_pMemoQueryWidget->doBtnSearch();
    m_strCurID = _dlg.getID();
    this->m_pMemoDetailInfoWidget->fillDate(this->m_strCurID);
    this->m_pAlarmDialogV1->updateAlarms();


}

void MemoMainWindow::doBtnDelete()
{
    MemoDetailInfoDialog _dlg(this->m_pMemoDBConnection->getConnection(),
                              m_strCurID,
                              MemoDetailInfoDialog::Delete,
                              this);
    _dlg.exec();
    this->m_pMemoQueryWidget->doBtnSearch();
    m_strCurID = _dlg.getID();
    this->m_pMemoDetailInfoWidget->fillDate(this->m_strCurID);
    this->m_pAlarmDialogV1->updateAlarms();
//    this->m_pMemoDetailInfoWidget->fillDate("");
}

void MemoMainWindow::doBtnModify()
{
    MemoDetailInfoDialog _dlg(this->m_pMemoDBConnection->getConnection(),
                              m_strCurID,
                              MemoDetailInfoDialog::Modify,
                              this);
    _dlg.exec();
    this->m_pMemoQueryWidget->doBtnSearch();
    m_strCurID = _dlg.getID();
    this->m_pMemoDetailInfoWidget->fillDate(this->m_strCurID);
    this->m_pAlarmDialogV1->updateAlarms();
}

void MemoMainWindow::doBtnShow()
{
    MemoDetailInfoDialog _dlg(this->m_pMemoDBConnection->getConnection(),
                              m_strCurID,
                              MemoDetailInfoDialog::Show,
                              this);
    _dlg.exec();
//    this->m_pMemoQueryWidget->doBtnSearch();
}

void MemoMainWindow::IDChanged(QString _strCurID)
{
    m_strCurID = _strCurID;
}

